package com.backedserver.controller.system;

import com.common.properties.JwtProperties;
import com.common.requestvo.system.MenuReqVo;
import com.common.service.system.MenuService;
import com.common.utils.JwtTokenUtils;
import com.common.utils.ResultVoUtil;
import com.common.vo.ResultVo;
import com.common.vo.system.MenuResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * MenuController
 *
 * @author johnny
 * @create 2020-07-10 下午2:17
 **/
@Api(tags = "菜单模块")
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public ResultVo add(@RequestBody MenuReqVo menuReqVo, HttpServletRequest request) {
        log.info("【MenuReqVo： {} 】", menuReqVo);
        //可以通过 拦截所有的请求 放入 vo中 操作人
        menuReqVo.setOperator(JwtTokenUtils.getUserNameFromReq(request));
        menuService.addMenu(menuReqVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改菜单")
    public ResultVo edit(@RequestBody MenuReqVo menuReqVo, HttpServletRequest request) {
        log.info("【MenuReqVo： {} 】", menuReqVo);
        menuReqVo.setOperator(JwtTokenUtils.getUserNameFromReq(request));
        menuService.editMenu(menuReqVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除菜单")
    public ResultVo delete(@PathVariable("id") Long id) {
        log.info("delete menu id : {}", id);
        menuService.deleteMenu(id);
        return ResultVoUtil.success();
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询菜单")
    public ResultVo list(@RequestParam("all") Integer all) {
        List<MenuResultVo> menuResultVoList;
        if (all == 1) {
            menuResultVoList = menuService.findAllMenuList(true);
        } else {
            menuResultVoList = menuService.findAllMenuList(false);
        }
        return ResultVoUtil.success(menuResultVoList);
    }


}